//
//  SplashDemoVC.m
//  JAGatherAdDemo
//
//  Created by Yc on 2022/4/24.
//

#import "SplashDemoVC.h"
#import <JAGatherAdSDK/JAGatherAdSDK.h>

@interface SplashDemoVC ()<JAGAdSplashDelegate>

@property (nonatomic, strong) JAGAdSplash *splash;
/// 广告未加载出来时的占位图
@property (nonatomic, strong) UIImageView *bgImgV;
/// 自定义底图
@property (nonatomic, strong) UIImageView *bottomView;

@end

@implementation SplashDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.placementList = [[NSMutableArray alloc] initWithArray:[self readPlacementDataOfType:DataType_splash]];
    [self setupPlacement:splashID];
    [self creatSplash];
}

- (void)creatSplash {
    [self deallocAd];
    self.splash = [[JAGAdSplash alloc] initWithPlacementID:[self getPlacement]];
    self.splash.delegate = self;
    self.splash.viewController = self;
    
    BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:SplashBottomViewkey];
    self.splash.bottomView = state ? self.bottomView : nil;
}

- (void)loadAdAction {
    [super loadAdAction];
    NSLog(@"%s", __FUNCTION__);
    TOAST(@"开始加载广告数据");
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgImgV];
    [self.splash loadAd];
    [self writePlacementDataOfType:DataType_splash];
}

- (void)showAdAction {
    [super showAdAction];
    NSLog(@"%s", __FUNCTION__);
    if (self.splash) {
        [self.splash showAd];
    }
}

- (void)loadAndShowAdAction {
    [super loadAndShowAdAction];
    NSLog(@"%s", __FUNCTION__);
    TOAST(@"开始加载广告数据");
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgImgV];
    [self.splash loadAndShowAd];
    [self writePlacementDataOfType:DataType_splash];
}

- (void)reselectPlacementAction {
    [self creatSplash];
}

- (void)deallocAd {
    self.splash = nil;
}

- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);
}

#pragma mark - JAGAdSplashDelegate

/// 开屏广告请求成功
- (void)JAGatherSplashAdSuccessLoad:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
    TOAST(@"数据加载完成");
    [self.bgImgV removeFromSuperview];
}
/// 开屏广告请求成功
/// @param isTimeout 加载是否超时
- (void)JAGatherSplashAdSuccessLoad:(JAGAdSplash *)splashAd timeout:(BOOL)isTimeout {
    NSLog(@"%s", __FUNCTION__);
    [self.bgImgV removeFromSuperview];
}
/// 开屏广告请求失败
- (void)JAGatherSplashAdFailLoad:(JAGAdSplash *)splashAd error:(NSError *)error {
    NSLog(@"%s", __FUNCTION__);
    NSString *msg = [NSString stringWithFormat:@"%@",error];
    TOAST(msg);
    [self.bgImgV removeFromSuperview];
}
/// 开屏广告曝光回调
- (void)JAGatherSplashAdExposured:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}
/// 开屏广告点击回调
- (void)JAGatherSplashAdClicked:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}
/// 开屏广告倒计时结束回调
- (void)JAGatherSplashAdCountdownToZero:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}
/// 开屏广告点击跳过回调
- (void)JAGatherSplashAdSkiped:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}
/// 开屏广告关闭回调
- (void)JAGatherSplashAdClosed:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}
/// 开屏广告关闭落地页回调
- (void)JAGatherSplashAdCloseLandingPage:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}
/// 广告加载超时
- (void)JAGatherSplashAdDidTimeout:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
    [self.bgImgV removeFromSuperview];
}

#pragma mark - Lazy
- (UIImageView *)bgImgV {
    if (!_bgImgV) {
        _bgImgV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _bgImgV.image = [UIImage imageNamed:@"BgImage"];
        _bgImgV.userInteractionEnabled = YES;
    }
    return _bgImgV;
}

- (UIImageView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 80)];
        _bottomView.image = [UIImage imageNamed:@"app_logo"];
    }
    return _bottomView;
}

@end
